package com.mad.game.view.level;

import java.util.Map;
import java.util.Random;

import com.mad.game.backend.GameDemo;
import com.mad.game.system.Vector2;
import com.mad.game.view.ViewManager;
import com.mad.game.view.models.GameModel;
import com.mad.game.view.models.ModelPool;

public class Level {

	private ModelPool modelPool ;
	private Camera camera ; 
	private boolean paused = false ; 
	private boolean canMoveCamera = false ; 
	
	private int mobSpawnTime = 0, mobSpawnTick = 0 ; 
	
	private String currentGroundTileID ; 

	public ModelPool getModelPool() {
		return modelPool;
	}
	
	public boolean isPaused() {
		return paused;
	}

	public void setPaused(boolean paused) {
		this.paused = paused;
	}

	public boolean canMoveCamera() {
		return canMoveCamera;
	}

	public void setCanMoveCamera(boolean canMoveCamera) {
		this.canMoveCamera = canMoveCamera;
	}

	public Level() {
		this.modelPool = new ModelPool(); 
		this.camera = new Camera(); 
		this.currentGroundTileID = ModelPool.GROUND1 ; 
		this.spawnPlayer(); 
	}

	public void update() {
		if(!this.isPaused()){
			if(mobSpawnTick == mobSpawnTime){
				mobSpawnTick = 0 ; 
				mobSpawnTime = new Random().nextInt(500) + 100 ; 
				this.spawnRandomMob();
			}else
				mobSpawnTick++ ; 
			modelPool.update(); 
			if(this.canMoveCamera())
				this.camera.moveRight();
			if(this.canMoveNextGroundTile())
				this.moveGroundTile(); 
		}
	}

	public boolean canMoveNextGroundTile(){
		return this.camera.getPosition().getX() % ViewManager.MAIN_WND_WIDTH == 0 ; 
	}
	
	private void moveGroundTile() {
		GameDemo.getGameInstance().getModelById(currentGroundTileID).move(
				new Vector2(ViewManager.MAIN_WND_WIDTH*3, 0)); 
		this.currentGroundTileID = ModelPool.getNextGroundTileID(currentGroundTileID);
	}

	public Map<String, GameModel> getModels() {
		return modelPool.getModels();
	}

	public void spawnPlayer() {
		modelPool.spawnPlayer();
	}
	
	public void spawnRandomMob(){
		modelPool.spawnMob();
	}

	public GameModel getModelById(String modelID) {
		return modelPool.getModelById(modelID);
	}

	public GameModel doesModelCollide_inMovement(GameModel model) {
		return modelPool.doesModelCollide_inMovement(model);
	}

	public GameModel doesModelCollide(GameModel model) {
		return modelPool.doesModelCollide(model);
	}

	
	
	public void moveEntityLeft(String id) {
		modelPool.moveEntityLeft(id);
	}

	public void addNewModel(String id, GameModel model) {
		modelPool.addNewModel(id, model);
	}

	public void removeModel(String id) {
		modelPool.removeModel(id);
	}

	public void removeModel(GameModel model) {
		modelPool.removeModel(model);
	}

	public void moveEntityRight(String id) {
		modelPool.moveEntityRight(id);
	}

	public void jumpEntityInMove(String id) {
		modelPool.jumpEntityInMove(id);
	}

	public void jumpEntityStraightUp(String id) {
		modelPool.jumpEntityStraightUp(id);
	}

	public boolean doesEntityJump(String id) {
		return modelPool.doesEntityJump(id);
	}
		
	public void entityShoot(String id) {
		modelPool.entityShoot(id);
	}

	public void moveCameraLeft() {
		camera.moveLeft();
	}

	public void moveCameraRight() {
		camera.moveRight();
	}

	public Vector2 getCameraPosition() {
		return camera.getPosition();
	}

	public boolean doesCameraMove() {
		return camera.velocity.equals(new Vector2(0,0));
	}
	
	
}
